#include "IS.h"
#include <stdlib.h>
#include <time.h>
#include "RSManager.h"
#include "Box.h"
#include "QuadTree.h"
#include "ShareComponents.h"
#include <list>
using namespace std;
#include "QuadTree.h"
#include "StaticObject.h"
#include "GameDefine.h"
IS::~IS(void)
{
}

IS::IS(Box* box): Object(box, ObjEnemy_DYNAMID), MoveObject()
{
	_nextMove = new Box(D3DXVECTOR2(),D3DXVECTOR2(),D3DXVECTOR2());
	current = new Box(D3DXVECTOR2(),D3DXVECTOR2(),D3DXVECTOR2());
	init();
}

void IS::init()
{

	RSManager* rs = RSManager::getInstance();
	this->left = rs->getSprite(IS_GO_LEFT);	
	this->right = rs->getSprite(IS_GO_RIGHT);
	this->jump_left = rs->getSprite(IS_JUMP_LEFT);
	this->jump_right = rs->getSprite(IS_JUMP_RIGHT);
	sink = rs->getSprite(SINK);
	timeDelay =0;	
	this->AssignMove(_box);		
	_box->_v.y = 0;	
	if(_box->_v.x > 0)
	{
		this->_sprite = right;
	}
	else
	{
		this->_sprite = left;
	}
	_box->_size = _sprite->getRenderSize();
	_sprite = _box->_v.x > 0.0f?jump_right:jump_left;
	_nextMove->_size = _box->_size;
	_nextMove->_v = _box->_v;
	dieTime = 0;
	_hits = 1;
	_colBox->_size = _box->_size;
	_colBox->_size.y -= 5;
}

void IS::update()
{
	
	if(!_isAlive) return;
	if(_hits <= 0)
	{
		_box->_v.y = 6.0f;
		_box->_v.x = 0;
		move();
		dieTime+=GAME_TIME;
		if(dieTime > 150)
		{
			_isAlive = false;
		}
		return;
	}
	move();		
	timeDelay += GAME_TIME;
	if(timeDelay > IS_CHANG_SPRITE)
	{
		timeDelay = 0;
		this->_sprite->Next();
	}
	if(isOnGround() || isOnOthers())
	{
		_sprite = _direction == 1 ? right : left;

	}
	else if(isOnWater())
	{
		_box->_v.x = 0;
		_sprite = sink;
		_isAlive = false;
	}

	bool chk_left = false;
	bool chk_right = false;
	if(this->checkOutGround())
	{
		StaticObject* G = _getCurrentGround();
		int delta = 10;

		current->_position = G->getBox()->_position;
		current->_size = G->getBox()->_size;
		current->_v = G->getBox()->_v;
		current->_position.x -= delta;
		current->_size.x += delta;

		list<StaticObject*> test =  gl_Quadtree->query_staticObj(current);
		for (StaticObject* i : test)
		{
			if(i == G) continue;
			if(i->getBox()->getRight() > G->getBox()->getRight())
				chk_right = true;
			if(i->getBox()->getLeft() < G->getBox()->getLeft())
				chk_left = true;
		}	
		if(_direction == 1 && !chk_right)
		{
			if(isOnGround())
			{
				_box->_v.y = rand() % 8;
				_sprite = _direction == 1 ? jump_right : jump_left;
			}
		}
		if(_direction == -1 && !chk_left)
		{
			if(isOnGround())
			{				
				int decision = rand() % 3;
				if(decision > 0)
				{
					_box->_v.y = rand() % 8;
					_sprite = _direction == 1 ? jump_right : jump_left;
				}
				else
				{
					_direction *= -1;
					_box->_v.x = _direction*IS_MOVE_VEC;
				}
			}
		}

	}

}

void IS::render()
{
	this->_sprite->Render(_box->_position);
}

Box* IS::getNextMoveBox()
{
	_nextMove->_size = _box->_size;
	if(_box->_v.x > 0.0f)
	{
		_nextMove->_position.x = _box->_position.x  + _box->_v.x + _box->_size.x;		
		_nextMove->_position.y = _box->_position.y + _box->_v.y;
	}	
	else
	{
		_nextMove->_position.x = _box->_position.x + _box->_v.x - _box->_size.x;
		_nextMove->_position.y = _box->_position.y + _box->_v.y;
	}
	return _nextMove;
}

Box* IS::getCollisionBox()
{
	
	_colBox->_v = _box->_v;
	_colBox->_position = _box->_position;
	_colBox->_position.x += _colBox->_size.x / 4;
	_colBox->_size.x /= 2;
	_colBox->_position.y += 5;
	_colBox->_v.y = (isOnSpace()) ? _colBox->_v.y : 0;
	return _colBox;
}

